package workshop.main;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import workshop.model.Address;
import workshop.model.Category;
import workshop.model.Product;
import workshop.model.ShoppingBasket;
import workshop.model.Supplier;
import workshop.model.User;

public class MainClass {
	public static void main(String[] args) {
//		ApplicationContext applicationcontext = new ClassPathXmlApplicationContext("mySpringPersistenceUnit");
//		Batch batch = (Batch) applicationcontext.getBean("batch");
//		batch.run();
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		
		
		// USERs + SB'S
		Address alexAddress = new Address("Av Chazal", "2", "1030", "Belgium");
		User alex = new User(alexAddress, "Alex", "1982-12-9");
		ShoppingBasket alexsb = new ShoppingBasket("2014-10-13", alex);

		Address georgeAddress = new Address("Av Lousie", "45", "1080", "Belgium");
		User george = new User(georgeAddress, "George", "1974-11-29");
		ShoppingBasket georgesb = new ShoppingBasket("2014-10-10", george);

		Address philAddress = new Address("Av Meiser", "450", "6300", "Belgium");
		User phil = new User(philAddress, "Phil", "1994-11-29");
		ShoppingBasket philsb = new ShoppingBasket("2014-8-10", phil);

		// THE SUPPLIERS
		Address appleAddress = new Address("California", "4", "4350", "USA");
		Supplier appleSupplier = new Supplier("Apple", appleAddress);

		Address hpAddress = new Address("Texas", "5", "4000", "USA");
		Supplier hpSupplier = new Supplier("HP", hpAddress);

		Address microsoftAddress = new Address("Brooklyn", "6", "3500", "USA");
		Supplier microsoftSupplier = new Supplier("Microsoft", microsoftAddress);

		Address samsungAddress = new Address("Yong", "7", "3000", "China");
		Supplier samsungSupplier = new Supplier("Samsung", samsungAddress);
		
		
		// THE CATEGORYS
		Category c1 = new Category("Computers");
		Category c2 = new Category("Notebooks");
		Category c3 = new Category("Tablets");
		Category c4 = new Category("Software");
		Category c5 = new Category("Mobile Phones");
		Category c6 = new Category("Others");
		Category c7 = new Category("Mobile Devices");

		// THE PRODUCTS
		Product p1 = new Product("MacBook", 2800);
		Product p2 = new Product("Windows XP", 80);
		Product p3 = new Product("Iphone 5S", 800);
		Product p4 = new Product("iPad", 700);
		Product p5 = new Product("Galaxy Tab", 400);
		Product p6 = new Product("Hp DeskJet 830C", 50);

		// ADDING PRODUCTS INTO SUPPLIERS
		appleSupplier.addProduct(p1);
		p1.setSupplierProductOwner(appleSupplier);
		
		appleSupplier.addProduct(p3);
		p3.setSupplierProductOwner(appleSupplier);
		
		appleSupplier.addProduct(p4);
		p4.setSupplierProductOwner(appleSupplier);
		
		microsoftSupplier.addProduct(p2);
		p2.setSupplierProductOwner(microsoftSupplier);
		
		hpSupplier.addProduct(p6);
		p6.setSupplierProductOwner(hpSupplier);
		
		samsungSupplier.addProduct(p5);
		p5.setSupplierProductOwner(samsungSupplier);
		
		// ADDING SUB-CATEGORYS INTO CATEGORYS
		c1.addChildren(c2);
		c2.setParent(c1);
		
		c7.addChildren(c3);
		c3.setParent(c7);
		
		c7.addChildren(c5);
		c5.setParent(c7);

		// ADDING PRODUCTS INTO CATEGORYS
		c2.addProduct(p1);
		p1.setCategoryProductOwner(c2);
		
		c4.addProduct(p2);
		p2.setCategoryProductOwner(c4);
		
		c5.addProduct(p3);
		p3.setCategoryProductOwner(c5);
		
		c3.addProduct(p4);
		p4.setCategoryProductOwner(c3);
		
		c3.addProduct(p5);
		p5.setCategoryProductOwner(c3);
		
		c6.addProduct(p6);
		p6.setCategoryProductOwner(c6);

		// ADDING SHOPPINGBASKET INTO USER'S
		george.addShoppingBasket(georgesb);
		georgesb.setUser(george);
		
		alex.addShoppingBasket(alexsb);
		alexsb.setUser(alex);
		
		phil.addShoppingBasket(philsb);
		philsb.setUser(phil);

		// ADDING PRODUCTS INTO SHOPPINGBASKET'S
		alexsb.setProductListSB(p3);
		p3.setSbProductContainer(alexsb);
		alexsb.setProductListSB(p4);
		p4.setSbProductContainer(alexsb);
		
		georgesb.setProductListSB(p1);
		p1.setSbProductContainer(georgesb);
		
		//OBJECT'S PERSIST
		em.persist(p6);
		em.persist(p5);
		em.persist(p4);
		em.persist(p3);
		em.persist(p2);
		em.persist(p1);
		em.persist(c1);
		em.persist(c7);
		em.persist(c6);
		em.persist(c5);
		em.persist(c4);
		em.persist(c3);
		em.persist(c2);
		em.persist(samsungSupplier);
		em.persist(microsoftSupplier);
		em.persist(hpSupplier);
		em.persist(appleSupplier);
		em.persist(philsb);
		em.persist(phil);
		em.persist(georgesb);
		em.persist(george);
		em.persist(alexsb);
		em.persist(alex);

		em.getTransaction().commit();
		
		// /*********** QUERY **********/
		System.out.println("\n****** Cherchez tous les produits qui appartiennent à une catégorie. ******\n");

		String tab = "Tablets";
		Category resul = (Category) em
				.createQuery("SELECT c FROM Category c where c.name= :Tablets")
				.setParameter("Tablets", tab).getSingleResult();
		
		
		List<Product> resultsByCategory = em
				.createQuery("SELECT p FROM Product p where p.categoryProductOwner.categoryId = :categoryId")
				.setParameter("categoryId", resul.getCategoryId()).getResultList();
		
		for (Product product : resultsByCategory) {
			System.out.println("\nSupplier: "+product.getSupplierProductOwner().getName());
			System.out.println("Category: "+product.getCategoryProductOwner().getName());
			System.out.println("Product: "+product.getName()+"\nPrice: "+product.getPrice()+"€");
		}
	
		
		// /*********** QUERY **********/
		System.out.println("\n****** Dans chaque cas, afficher la hiérarchie de catégories à laquelle le produit appartient. ******\n");
		
		List<Product> allProducts = em.createQuery("SELECT p FROM Product p").getResultList();
		for (Product product : allProducts) {
			System.out.println("Product: "+product.getName()+"\nCategory: "+product.getCategoryProductOwner().getName());
		}
		
		
		// /*********** QUERY **********/
		System.out.println("\n****** Récupérez tous les produits qui appartiennent à une catégorie ou à ses sous-catégories. ******\n");

		List<Product> allProductsSubCategorys = em
				.createQuery("SELECT p FROM Product p JOIN p.categoryProductOwner c WHERE c.parent.categoryId > :valeur")
				.setParameter("valeur", (long)0).getResultList();
				
		for (Product product : allProductsSubCategorys) {
			System.out.println("\nCategory: "+product.getCategoryProductOwner().getParent().getName());
			System.out.println("Sub Category: "+ product.getCategoryProductOwner().getName());
			System.out.println("Product: "+product.getName());
		}
		
		// /*********** QUERY **********/
		System.out.println("\n****** Listez tous les fournisseurs de produits onéreux (produits dont le prix est supérieur à une valeur donnée). ******\n");
		double price = 500;
		
		List<Supplier> expensiveProducts = em.createQuery("SELECT p.supplierProductOwner FROM Product p WHERE p.price >= :price ").setParameter("price", price).getResultList();

		for (Supplier supplier : expensiveProducts) {
			System.out.println(supplier.getName());
		}				

		// /*********** QUERY **********/
		System.out.println("\n****** Récupérez tous les panier contenant un produit donné. ******\n");
				
		String prod = "MacBook";
		
		
				
				
				
				
				
		em.close();
		emf.close();
	}

}
